from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import ModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from areas.models import Area
from areas.serializers import AreaSerializer, SubAreaSerializer


# CacheResponseMixin使用缓存减少查询数据库的次数，设置有效期，避免数据库操作而缓存数据未变
class AreasViewSet(ModelViewSet, CacheResponseMixin):

    pagination_class = None   # 关闭分页

    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer

